﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;

namespace AppManagement.BusManagers
{
    #region Using
    using AppManagement.Models;
    using AppManagement.Repositories;
    using AppManagement.Helpers;
    #endregion

    /// <summary>
    ///  insert new khenthuong progress
    /// </summary>
    public partial class FrmCapNhatChiSo : DevComponents.DotNetBar.Office2007Form
    {
        public EventHandler Handler { get; set; }

        public FrmCapNhatChiSo(string madichvu)
        {
            InitializeComponent();

            txtMaDichVu.Text = madichvu;
            var item = DichVuRepository.SelectByID(madichvu);
            txtTenDichVu.Text = item.ToString();
        }

        public void LoadData()
        {

        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnThemVaoDanhSachDV_Click(object sender, EventArgs e)
        {
            FrmChonChiSo frm = new FrmChonChiSo();
            frm.Handler += ThemVaoDanhSachDV;
            frm.ShowDialog();
        }

        public void ThemVaoDanhSachDV(object sender, EventArgs e)
        {
            var eventType = (TransferSimpleDataEvent)e;
            string[] comp = eventType.Data.Split(new char[] { '#' });

            var chiso = ChiSoRepository.SelectByID(comp[0]);

            var objListViewItem = new ListViewItem();
            objListViewItem.Text = comp[0];
            objListViewItem.SubItems.Add(chiso.TenCS);
            objListViewItem.SubItems.Add(comp[1]);
            objListViewItem.SubItems.Add(chiso.GhiChu);
            lstvDanhSachDV.Items.Add(objListViewItem);
        }

        private void btnXoaKhoiDanhSachDV_Click(object sender, EventArgs e)
        {
            if (lstvDanhSachDV.SelectedItems.Count > 0)
            {
                lstvDanhSachDV.Items.RemoveAt(lstvDanhSachDV.SelectedIndices[0]);
            }
        }

        private void btnGhi_Click(object sender, EventArgs e)
        {
            var success = false;

            if (ActionUpdate())
            {
                success = true;
            }

            // Inform result for nguoidung
            if (success)
            {
                LoadData();
                MessageBox.Show("Cập nhật dữ liệu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("Cập nhật dữ liệu thất bại", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Update an item in DB
        /// </summary>
        /// <returns></returns>
        private bool ActionUpdate()
        {
            try
            {
                // Removew old phieukham_thuoc
                var lstItem2 = ChiSo_DichVuRepository.SelectByMaDV(txtMaDichVu.Text);
                foreach (var item in lstItem2)
                {
                    if (!ChiSo_DichVuRepository.Delete(item.MaCSDV))
                    {
                        return false;
                    }
                }

                // add new phieukham_dv
                for (int i = 0; i < lstvDanhSachDV.Items.Count; i++)
                {
                    var chiso_dv = new ChiSo_DichVu
                    {
                        MaCS = lstvDanhSachDV.Items[i].Text,
                        MaDV = txtMaDichVu.Text,
                        GiaTri = lstvDanhSachDV.Items[i].SubItems[2].Text,
                    };
                    if (!ChiSo_DichVuRepository.Insert(chiso_dv))
                    {
                        return false;
                    }
                }

                return true;
            }
            catch
            {
                return false;
            }
        }

        private void FrmCapNhatChiSo_Load(object sender, EventArgs e)
        {
            var lstItem = ChiSo_DichVuRepository.SelectByMaDV(txtMaDichVu.Text);
            lstvDanhSachDV.Items.Clear();
            for (int i = 0; i < lstItem.Count; i++)
            {
                var objListViewItem = new ListViewItem();
                objListViewItem.Text = lstItem[i].MaCS;
                objListViewItem.SubItems.Add(lstItem[i].ChiSo.TenCS);
                objListViewItem.SubItems.Add(lstItem[i].GiaTri);
                objListViewItem.SubItems.Add(lstItem[i].ChiSo.GhiChu);
                lstvDanhSachDV.Items.Add(objListViewItem);
            }
        }
    }
}